import  {doLoginApi}  from '@/api/loginApi'
import { setToken,delToken } from '@/utils/token'
import { setName,delName } from '@/utils/name'
import { setRole,delRole } from '@/utils/role'
import { setMenu,delMenu } from '@/utils/menu'
export default{
    state:{
        uname:'',
        role:'',
        menu:[],
        token:'',
        uid:''
    },
    mutations:{
        addName(state,name){
            state.uname = name
            setName(name)
        },
        addRole(state,role){
            state.role = role
            setRole(role)
        },
        addToken(state,token){
            state.token = token
            setToken(token)
        },
        addMenu(state,menu){
            state.menu = menu
            setMenu(menu)
        },
        addUid(state,uid){
            state.uid = uid
        },
        logout(state,a){
            state.uname = ''
            state.role = ''
            state.menu = []
            state.token = ''
            state.uid = ''
            delToken()
            delName()
            delRole()
            delMenu()
        }
    },
    actions:{
        async dologin({ commit },userData){
            let ret = await doLoginApi(userData)
            if(ret.backMsg.code === 0){
                commit('addName',ret.backMsg.name)
                commit('addRole',ret.backMsg.role)
                commit('addToken',ret.backMsg.token)
                commit('addMenu',ret.backMsg.menu)
                commit('addUid',ret.backMsg.uid)
            }
            return ret.backMsg.code === 0 ? true :false
        }
    }
}